package com.mb.ext.core.service;

import com.mb.ext.base.BaseRepository;
import com.mb.ext.base.BaseService;
import com.mb.ext.base.PageHelp;
import com.mb.ext.core.dao.FileDAO;
import com.mb.ext.core.dao.NewProductDao;
import com.mb.ext.core.dao.product.ProductSkuDAO;
import com.mb.ext.core.entity.FileEntity;
import com.mb.ext.core.entity.product.ProductEntity;
import com.mb.ext.core.service.spec.product.ProductDTO;
import com.mb.framework.exception.DAOException;
import com.mb.framework.util.CollectionUtil;
import com.mb.framework.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.List;

/**
 * @author sunhao~
 * @version 1.0
 * @date 2021/7/10 11:11
 */
@Service
public class NewProductServiceImpl extends BaseService<ProductEntity> {
  @Autowired
  private NewProductDao newProductDao;
  @Autowired
  private EntityManager entityManager;
  @Autowired
  private FileDAO fileDao;
  @Autowired
  private ProductSkuDAO productSkuDAO;

  @Override
  protected BaseRepository<ProductEntity> getBaseRepository() {
    return newProductDao;
  }


  /**
   * 查询商品
   */
  public Page<HashMap<String, Object>> getHomeProduct(Integer page, Integer size, String search) throws DAOException {
    PageHelp pageHelp = new PageHelp();
    pageHelp.setPageNumber(page);
    pageHelp.setPageSize(size);
    StringBuffer countBuffer = new StringBuffer();
    countBuffer.append(" select count(1)  ");
    String sql = " from  product where IS_ON_SALE = 1 and IS_DELETED=0 and (IS_HOT=1 or IS_RECOMMEND=1) ";
    if (StringUtils.isNotEmpty(search)) {
      sql = sql + " and  PRODUCT_NAME like " + "'%" + search + "%'" + " ";
    }
    StringBuffer selectBuffer = new StringBuffer();
    selectBuffer.append(" select PRODUCT_UUID productId,PRODUCT_NAME productName,PRODUCT_MAIN_IMAGE_UUID imageId, UNIT_PRICE productPrice,UNIT_PRICE_STANDARD orangeProductPrice,SOLDUNIT soldunit  ");
    Page<HashMap<String, Object>> pageData = findPage(entityManager, countBuffer.toString() + "" + sql,
        selectBuffer.toString() + "" + sql
        , pageHelp.getPage(), pageHelp.getPageNumber(), pageHelp.getPageSize());
    if (!CollectionUtil.isEmpty(pageData.getContent())) {
      for (HashMap<String, Object> map : pageData.getContent()) {
        Object productId = map.get("productId");
        Object imageId = map.get("imageId");
        if (imageId != null) {
          FileEntity fileByUuid = fileDao.getFileByUuid(imageId.toString());
          map.put("imageUrl", fileByUuid != null ? fileByUuid.getUrl() : "");
        }
        List<HashMap<String, Object>> productSkuList = findBySql(entityManager, "select PRODUCTSKU_UUID  productSkuUUId,SKU_CODE scuCode,SKU_UNIT_POINT scuUnItPoint from product_sku where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
        map.put("productSkuList", productSkuList);
        List<HashMap<String, Object>> productGuy = findBySql(entityManager, "select GROUP_BUY_PRODUCT_UUID  groupBuyProductUuId from group_buy_product where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
        map.put("groupByproduct", CollectionUtils.isNotEmpty(productGuy) ? productGuy.get(0) : new HashMap<>());
      }
    }
    return pageData;
  }


  public List<HashMap<String, Object>> getAllProductCate() throws DAOException {
    List<HashMap<String, Object>> bySql = findBySql(entityManager, "select PRODUCTCATE_UUID  productCateUuid,CATE_NAME cateName,CATE_PIC_URL catePicUrl from product_cate where IS_DELETED=0 order by SORTNUMBER asc");
    if (!CollectionUtil.isEmpty(bySql)) {
      for (HashMap<String, Object> mapOne : bySql) {
        Object productCateUuid = mapOne.get("productCateUuid");
        List<HashMap<String, Object>> productMap = findBySql(entityManager, "select PRODUCT_UUID productId,PRODUCT_NAME productName ,SOLDUNIT soldunit,UNIT_PRICE productPrice,PRODUCT_MAIN_IMAGE_UUID imageId from product where IS_ON_SALE = 1 and IS_DELETED=0 and PRODUCTCATE_UUID=" + "'" + productCateUuid + "'" + " ");

        for (HashMap<String, Object> map : productMap) {
          Object productId = map.get("productId");
          Object imageId = map.get("imageId");
          if (imageId != null) {
            FileEntity fileByUuid = fileDao.getFileByUuid(imageId.toString());
            map.put("imageUrl", fileByUuid != null ? fileByUuid.getUrl() : "");
          }
          List<HashMap<String, Object>> productSkuList = findBySql(entityManager, "select PRODUCTSKU_UUID  productSkuUUId,SKU_CODE scuCode,SKU_UNIT_POINT scuUnItPoint from product_sku where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
          map.put("productSkuList", productSkuList);
          List<HashMap<String, Object>> productGuy = findBySql(entityManager, "select GROUP_BUY_PRODUCT_UUID  groupBuyProductUuId from group_buy_product where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
          map.put("groupByproduct", CollectionUtils.isNotEmpty(productGuy) ? productGuy.get(0) : new HashMap<>());
        }

        mapOne.put("productList", productMap);
      }
    }
    return bySql;
  }

  public Page<HashMap<String, Object>> getAllProductByCateId(Integer page, Integer size, String productCateId) throws DAOException {
    List<HashMap<String, Object>> bySql = findBySql(entityManager, "select PRODUCTCATE_UUID  productCateUuid,CATE_NAME cateName,CATE_PIC_URL catePicUrl from product_cate where IS_DELETED=0  and PRODUCTCATE_UUID=" + "'" + productCateId + "'" + " order by SORTNUMBER asc");
    if (!CollectionUtils.isEmpty(bySql)) {
      Object productCateUuid = bySql.get(0).get("productCateUuid");
      PageHelp pageHelp = new PageHelp();
      pageHelp.setPageNumber(page);
      pageHelp.setPageSize(size);
      Page<HashMap<String, Object>> pageData = findPage(entityManager, "select count(1) from  product where IS_ON_SALE = 1 and IS_DELETED=0 and PRODUCTCATE_UUID=" + "'" + productCateUuid + "'" + " ", "select PRODUCT_UUID productId,PRODUCT_NAME productName ,SOLDUNIT soldunit,UNIT_PRICE productPrice,PRODUCT_MAIN_IMAGE_UUID imageId from product where IS_ON_SALE = 1 and IS_DELETED=0 and PRODUCTCATE_UUID=" + "'" + productCateUuid + "'" + " ", pageHelp.getPage(), pageHelp.getPageNumber(), pageHelp.getPageSize());
      if (!CollectionUtil.isEmpty(pageData.getContent())) {
        for (HashMap<String, Object> map : pageData.getContent()) {
          Object productId = map.get("productId");
          Object imageId = map.get("imageId");
          if (imageId != null) {
            FileEntity fileByUuid = fileDao.getFileByUuid(imageId.toString());
            map.put("imageUrl", fileByUuid != null ? fileByUuid.getUrl() : "");
          }
          List<HashMap<String, Object>> productSkuList = findBySql(entityManager, "select PRODUCTSKU_UUID  productSkuUUId,SKU_CODE scuCode,SKU_UNIT_POINT scuUnItPoint from product_sku where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
          map.put("productSkuList", productSkuList);
          List<HashMap<String, Object>> productGuy = findBySql(entityManager, "select GROUP_BUY_PRODUCT_UUID  groupBuyProductUuId from group_buy_product where PRODUCT_UUID=" + "'" + productId + "'" + " and IS_DELETED=0  ");
          map.put("groupByproduct", CollectionUtils.isNotEmpty(productGuy) ? productGuy.get(0) : new HashMap<>());
        }
      }
      return pageData;
    }
    return Page.empty();
  }
}
